package com.example.demo;




import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;

import java.io.*;
import java.util.List;


/**
 * @program:
 * @InterfaceName: TaskBookController
 * @description: 任务书管理导出任务书-控制层
 * @Author: han_jun@hoperun.com
 * @Date: 2020/11/18
 */
@RestController
@RequestMapping(value = "/test")
public class TaskBookController {


    private final static Logger logger = LoggerFactory.getLogger(TaskBookController.class);


    /**
     * 任务书管理-合同查询 导出合同模板  word
     *
     * @param response
     * @throws Exception
     */
    @PostMapping(value = "/exportWord")
    public void getContractWord(HttpServletResponse response) throws Exception {


        ExportTaskBookWord exportWord = new ExportTaskBookWord();

        String path = exportWord.getContractWordList("/updateFile/");
        //导出word
        OutputStream ous = null;
        InputStream ins = null;
        File file = new File(path);
        try {
            ins = new BufferedInputStream(new FileInputStream(file));
            byte[] buffer = new byte[ins.available()];
            ins.read(buffer);
            response.reset();
            response.setHeader("Content-Disposition", "attachment; filename=" + file.getName());
            response.addHeader("Content-Length", "" + file.length());
            ous = new BufferedOutputStream(response.getOutputStream());

            response.setContentType("application/msword");

            ous.write(buffer);
            ous.flush();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                ous.close();
                ins.close();
                file.delete();
                //删除文件
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }


}
